import 'package:flutter/material.dart';

import 'package:get/get.dart';

import '../controllers/cart_controller.dart';
import '../../../utils/screenAdapter.dart';
import './card_item_view.dart';

class CartView extends GetView {
  @override
  CartController controller = Get.put<CartController>(CartController());
  // 注意：如果CartView在多个地方调用，需要在每个地方都手动Put一个CartController实例，否则会导致多个实例的问题

  CartView({super.key});
  Widget _pageBody() {
    return Container(
      padding: EdgeInsets.fromLTRB(
        ScreenAdapter.width(20),
        ScreenAdapter.height(20),
        ScreenAdapter.width(20),
        ScreenAdapter.height(210),
      ),
      child: ListView(
        children: controller.cartList
            .map((item) => CardItemView(item))
            .toList(),
      ),
    );
  }

  Widget _bottomBar() {
    return Positioned(
      left: 0,
      right: 0,
      bottom: 0,
      child: Container(
        padding: EdgeInsets.fromLTRB(
          0,
          ScreenAdapter.height(30),
          ScreenAdapter.width(20),
          ScreenAdapter.height(30),
        ),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(top: BorderSide(width: 1, color: Colors.black12)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Obx(
                  () => Checkbox(
                    value: controller.isAllChecked.value,
                    // activeColor: Colors.orange,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(
                        ScreenAdapter.radius(30),
                      ),
                    ),
                    side: BorderSide(width: 1, color: Colors.black12),
                    onChanged: (value) {
                      controller.checkedAllGoods(value!);
                      print("checkbox $value");
                    },
                  ),
                ),
                Text("全选", style: TextStyle(fontWeight: FontWeight.bold)),
              ],
            ), // 左边
            Obx(
              () => !controller.isEdit.value
                  ? Row(
                      children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: [
                            Row(
                              children: [
                                Text("合计"),
                                Text(
                                  "(不含运费): ",
                                  style: TextStyle(
                                    color: Colors.black38,
                                    fontSize: ScreenAdapter.fontSize(24),
                                  ),
                                ),
                                Text(
                                  "¥98.89",
                                  style: TextStyle(
                                    color: Colors.orange,
                                    fontSize: ScreenAdapter.fontSize(40),
                                  ),
                                ),
                              ],
                            ),
                            Row(
                              children: [
                                Text(
                                  "免运费",
                                  style: TextStyle(
                                    color: Colors.black38,
                                    fontSize: ScreenAdapter.fontSize(24),
                                  ),
                                ),
                                Text(
                                  "优惠:¥0.00 ",
                                  style: TextStyle(
                                    color: Colors.black38,
                                    fontSize: ScreenAdapter.fontSize(24),
                                  ),
                                ),
                                Text(
                                  "明细>",
                                  style: TextStyle(
                                    color: Colors.orange,
                                    fontSize: ScreenAdapter.fontSize(24),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                        SizedBox(width: ScreenAdapter.width(20)),
                        ElevatedButton(
                          onPressed: () {
                            // 判断是否登录
                            // Get.toNamed("/checkout");
                            controller.checkout();
                          },
                          style: ButtonStyle(
                            padding: WidgetStateProperty.all(
                              EdgeInsets.symmetric(
                                horizontal: ScreenAdapter.width(60),
                                vertical: ScreenAdapter.height(40),
                              ),
                            ),
                            backgroundColor: WidgetStateProperty.all(
                              Colors.orange,
                            ),
                            foregroundColor: WidgetStateProperty.all(
                              Colors.white,
                            ),
                          ),
                          child: Text(
                            "结算(${controller.checkedGoodsCount.value})",
                          ),
                        ),
                      ],
                    )
                  : ElevatedButton(
                      onPressed: () {
                        // 删除选中商品
                        controller.deleteCheckedGoods();
                      }, //结算按钮
                      style: ButtonStyle(
                        backgroundColor: WidgetStateProperty.all(Colors.orange),
                        foregroundColor: WidgetStateProperty.all(
                          Colors.white,
                        ),
                      ),
                       child: Text("删除"),
                    ),
            ), // 右边
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('购物车'),
        actions: [
          TextButton(
            onPressed: () {
              controller.changeEditStatus();
            },
            child: Obx(
              () => Text(
                controller.isEdit.value ? "完成" : "编辑",
                style: TextStyle(color: Colors.black38),
              ),
            ),
          ),
        ],
      ),
      body: GetBuilder<CartController>(
        // 注意：由于cart这个页面的controller绑定在tabBar,所以对应controller的onInit只会在加载首页时执行
        initState: (state) {
          // 如想在跳转到cart页面时执行初始化操作，需要在这里实现
          controller.getCartList(); // 获取购物车列表
          controller.calcCheckedCount(); // 计算结算数量
        },
        init: controller,
        builder: (controller) {
          return controller.cartList.isNotEmpty
              ? Stack(children: [_pageBody(), _bottomBar()])
              : Center(child: Text("购物车空空如也~"));
        },
      ),
    );
  }
}
